package main;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;


public class FileManager {

	
	
	 
	static HashMap<Integer,HashMap<Integer,ArrayList<MusicFile>>> calanderList = new  HashMap<Integer,HashMap<Integer,ArrayList<MusicFile>>>();
	static ConcurrentHashMap<String , MusicFile> nameToMusicFile = new ConcurrentHashMap<String , MusicFile>();

	
	public static ArrayList<MusicFile> getListForMonth(int selectedYear,
			int selectedMonth) throws IllegalArgumentException {
		
		if (selectedMonth > 12)
			throw new IllegalArgumentException("month trying to recieve > 12");
		try {
		if(!calanderList.containsKey(selectedYear) || !calanderList.get(selectedYear).containsKey(selectedMonth))
			return new ArrayList<MusicFile>();
		}
		catch(NullPointerException n ) {
			System.out.print("sf");
		}
		return calanderList.get(selectedYear).get(selectedMonth) ;
	}
	
	
	public static MusicFile getMusicFileFromName(String name) {

		return nameToMusicFile.get(name);
	}
	
	
	public static void addMusicFile(MusicFile fileToAdd) {
		nameToMusicFile.put(fileToAdd.name, fileToAdd);
		
		int year = fileToAdd.airDate.get(Calendar.YEAR);
		int month = fileToAdd.airDate.get(Calendar.MONTH);
		
		if(!calanderList.containsKey(year))
			calanderList.put(year, new HashMap<Integer,ArrayList<MusicFile>>());
		 
		if (!calanderList.get(year).containsKey(month))
			calanderList.get(year).put(month, new ArrayList<MusicFile>());
		
		calanderList.get(year).get(month).add(fileToAdd);	

		
	}
	
	
	
	public static void rename(String oldName, String newName) {
	 nameToMusicFile.get(oldName).name = newName;
		
	}


	public static MusicFile addWaveToSysten(File listCurrentlySelected, Calendar drop) {
		String name = listCurrentlySelected.getName().substring(0,listCurrentlySelected.getName().length()- 4);
		MusicFile musicFile = new MusicFile(1, drop, listCurrentlySelected,false, MusicType.WAVE, name, listCurrentlySelected.getParent());
		
		musicFile.transition = Transitions.WAVE_MP3;
		addMusicFile(musicFile);
		return musicFile;
		
	}
	
	
	 
}
